var StartPage={}
StartPage = (function(){return{
	init: function() {
		//this.strings = document.getElementById("startpage-strings");
		if(window.TMP_BrowserOpenTab){
			getBrowser().removeEventListener("NewTab",window.TMP_BrowserOpenTab,true);
			window.originalBrowserOpenTab=window.TMP_BrowserOpenTab;
			window.TMP_BrowserOpenTab=StartPage.newTab;
			getBrowser().addEventListener("NewTab",window.TMP_BrowserOpenTab,true)
		}
		else{
			if(window.TBP_BrowserOpenTab){
				getBrowser().removeEventListener("NewTab",window.TBP_BrowserOpenTab,true);
				window.originalBrowserOpenTab=window.TBP_BrowserOpenTab;
				window.TBP_BrowserOpenTab=StartPage.newTab;
				getBrowser().addEventListener("NewTab",window.TBP_BrowserOpenTab,true)
			}else{
				getBrowser().removeEventListener("NewTab",window.BrowserOpenTab,false);
				window.originalBrowserOpenTab=window.BrowserOpenTab;
				window.BrowserOpenTab=StartPage.newTab;
				getBrowser().addEventListener("NewTab",window.BrowserOpenTab,false)
			}
		}
		var self=this;
		gBrowser.addEventListener("TabSelect", function(){ self.tabChange.call(self) }, false);
		gBrowser.addEventListener("TabClose", function(){ setTimeout(function(){self.tabClose.call(self)},100) }, false);
		//gBrowser.addEventListener("TabOpen", function(){ self.tabChange.call(self) }, false);

		// The last value is a Mozilla-specific value to indicate untrusted content is allowed to trigger the event.
		window.addEventListener("load",function(){self.load.call(self)},false);
		// fired from html page
		document.addEventListener("startPageReady", function(e) { self.tabReady.call(self); }, false, true);
		document.addEventListener("refresh", function(e){ self.refresh.call(self); }, false, true); 
		 
		document.addEventListener("checkGmail", function(e){ self.Gmail.check.call(self.Gmail); }, false, true); 
		document.addEventListener("checkReader", function(e){ self.Reader.check.call(self.Reader); }, false, true); 
	}

	,tabClose: function(){ // when startpage tab is selected
		this.ClosedTabs.refresh.call(this.ClosedTabs);
	}
	
	,refresh: function(){
		this.ClosedTabs.refresh.call(this.ClosedTabs);
		this.MostPopular.refresh.call(this.MostPopular);
		
		this.Gmail.check.call(this.Gmail);
		this.Reader.check.call(this.Reader);
	}
	
	,tabChange: function(){ // when startpage tab is selected
		this.ClosedTabs.populate.call(this.ClosedTabs);
		this.MostPopular.populate.call(this.MostPopular);

		//write check-labels
		this.Gmail.updateLabel.call(this.Gmail);
		this.Reader.updateLabel.call(this.Reader);
	}
	
	,tabReady: function(){ // when html body loads 
		this.ClosedTabs.populate.call(this.ClosedTabs);
		this.MostPopular.populate.call(this.MostPopular);
		
		this.Reader.init.call(this.Reader); 
		this.Gmail.init.call(this.Gmail);
	}
	
	,newTab: function(){ window.originalBrowserOpenTab(); StartPage.load(); }
	
	,load: function(){
		var newLocationChange = window.URLBarSetURI.toString();
		newLocationChange = newLocationChange.replace(/aURI.spec == \"about:blank\"/g,'aURI.spec == "about:blank" || (aURI.spec.indexOf("chrome://startpage/content") == 0)');
		newLocationChange = newLocationChange.replace(/uri.spec == \"about:blank\"/g,'uri.spec == "about:blank" || (uri.spec.indexOf("chrome://startpage/content") == 0)');
		eval('window.URLBarSetURI = ' + newLocationChange + ';');
		if (window.gInitialPages) window.gInitialPages.push("chrome://startpage/content/startpage.html");
		gBrowser.loadURI("chrome://startpage/content/startpage.html",null,null,null,false,false);
      //if (gURLBar) setTimeout(function() { gURLBar.blur(); }, 0);
	}
	
}}())
function log(str){ Cc['@mozilla.org/consoleservice;1'].getService(Ci.nsIConsoleService).logStringMessage(str) }

Array.prototype.in_array = function(v){ for(var i=0,al=this.length;i<al;i++) if(this[i]==v)return i; return -1; }
